"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 统一数据库管理工具
Case Name   : 数据库主备实例切换,基础参数验证
Create At   : 2022-07-07
Owner       : opentestcase007
Description :
    1.查看数据库实例状态
    2.数据库主备实例切换
    3.查看数据库实例状态
    4.重置节点状态为初始配置状态
    5.查看数据库实例状态
    6.重置节点状态到指定AZ（指定primary节点到指定AZ
    7.查看数据库实例状态
    8.指定节点进行-f类型switchover
    9.重置节点状态为初始配置状态
Expect      :
    1.查看数据库实例状态成功
    2.数据库主备实例切换成功
    3.查看数据库实例状态成功
    4.重置节点状态为初始配置状态成功
    5.查看数据库实例状态成功
    6.重置节点状态到指定AZ（指定primary节点到指定AZ成功
    7.查看数据库实例状态成功
    8.指定节点进行-f类型switchover成功
    9.重置节点状态为初始配置状态成功
History     : 
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

Pri_U_SH = CommonSH('PrimaryDbUser')
Node_Num = Pri_U_SH.get_node_num()


@unittest.skipIf(3 > Node_Num, '不满足一主两备环境跳过')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.dbuser_node = Node('dbuser')
        self.log.info(f"-----{os.path.basename(__file__)} start-----")

    def test_server_tools1(self):
        text = '--step1:查看数据库实例状态 expect:查询成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl query -Cv'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        status = cmctl_cmd.split('\n')[-1]
        self.log.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.assertIn('6001     P Primary Normal', status)

        text = '--step2:数据库主备实例切换 expect:数据库主备实例切换成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl switchover -A'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: switchover -A successfully.', cmctl_cmd)

        text = '--step3:查看数据库实例状态 expect:查询成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl query -Cv'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        status = cmctl_cmd.split('\n')[-1]
        self.log.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.assertIn('6001     P Standby Normal', status)
        self.assertIn('6002     S Primary Normal', status)

        text = '--step4:重置节点状态为初始配置状态 expect:重置数据库状态成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl switchover -a'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: switchover successfully.', cmctl_cmd)

        text = '--step5:查看数据库实例状态 expect:查询成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl query -v -C -i -d'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        status = cmctl_cmd.split('\n')[-1]
        self.log.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)

        text = '--step6:重置节点状态到指定AZ（指定primary节点到指定AZ） expect:执行成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl switchover -z AZ1'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: switchover -z AZ1 successfully.', cmctl_cmd)

        text = '--step7:查看数据库实例状态 expect:查看成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl query -v -C -z ALL'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cluster_state   : Normal', cmctl_cmd)

        text = '--step8:指定节点进行-f类型switchover expect:指定节点切换为主节点--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl switchover -n 2 -D {macro.DB_INSTANCE_PATH} -f'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: switchover successfully.', cmctl_cmd)

    def tearDown(self):
        text = '--step9:重置节点状态为初始配置状态 expect:重置数据库状态成功--'
        self.log.info(text)
        cmctl_cmd = f'''source {macro.DB_ENV_PATH}
                    cm_ctl switchover -a'''
        self.log.info(cmctl_cmd)
        cmctl_cmd = self.dbuser_node.sh(cmctl_cmd).result()
        self.log.info(cmctl_cmd)
        self.assertIn('cm_ctl: switchover successfully.', cmctl_cmd)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
